package aash.catalog.views;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLayeredPane;

import org.apache.log4j.Logger;

import aash.catalog.controller.CatalogController;

/**
 * View for opening DB.
 * @author Aashintan
 */
public class OpenDbView extends AbstractView implements ActionListener {
    /**
     * Logger.
     */
    private static Logger log = Logger.getLogger(OpenDbView.class.getName());
    /**
     * The frame (window).
     */
    private JDialog frame = null;
    /**
     * The panel.
     */
    private JLayeredPane contentPane = null;
    /**
     * The ok button.
     */
    private JButton okButton = null;
    /**
     * The cancel button.
     */
    private JButton cancelButton = null;
    /**
     * The cancel button.
     */
    private JComboBox comboBox = null;

    /**
     * Class constructor.
     * @param ccController of the view
     */
    public OpenDbView(final CatalogController ccController) {
        super(ccController);
        buildFrame();
    }

    /**
     * @Override
     * @param ae event
     */
    public void actionPerformed(final ActionEvent ae) {
        if (ae.getActionCommand().equals(AashActionCommand.ACTION_CANCEL)) {
            close();
        }
        if (ae.getActionCommand().equals(AashActionCommand.ACTION_OK)) {
            getController().notifyOpenDb((String) comboBox.getSelectedItem());
            close();
        }
    }

    /**
     * Build the frame.
     */
    private void buildFrame() {
        frame = new JDialog();
        contentPane = new JLayeredPane();
        contentPane.setBorder(BorderFactory.createLoweredBevelBorder());
        // Adding the comboBox
        String[] dbList = getController().getDbList();
        comboBox = new JComboBox(dbList);
        comboBox.setBounds(30, 50, 200, 30);
        comboBox.addActionListener(this);
        contentPane.add(comboBox, JLayeredPane.PALETTE_LAYER);
        // Adding the ok button
        cancelButton = new JButton("Cancel");
        cancelButton.setBounds(30, 140, 80, 30);
        cancelButton.addActionListener(this);
        cancelButton.setActionCommand(AashActionCommand.ACTION_CANCEL);
        contentPane.add(cancelButton, JLayeredPane.PALETTE_LAYER);
        // Adding the cancel button
        okButton = new JButton("Ok");
        okButton.setBounds(150, 140, 80, 30);
        okButton.addActionListener(this);
        okButton.setActionCommand(AashActionCommand.ACTION_OK);
        contentPane.add(okButton, JLayeredPane.PALETTE_LAYER);
        // Frame operations
        frame.setBounds(300, 300, 500, 500);
        frame.setContentPane(contentPane);
        frame.setUndecorated(true);
        frame.setAlwaysOnTop(true);
        frame.setModal(true);
        frame.pack();
        frame.setSize(280, 200);
    }

    /**
     * Close method.
     * @Override
     */
    public final void close() {
        frame.setVisible(false);
        frame.setAlwaysOnTop(false);
        frame.dispose();
        getController().updateMainView();
    }

    /**
     * Display method.
     * @Override
     */
    public final void display() {
        frame.setVisible(true);
        frame.setAlwaysOnTop(true);
        frame.requestFocusInWindow();
    }
    
    /**
     * Refresh the list of databases available.
     */
    public void refreshDbList() {
    	String[] dbList = getController().getDbList();
    	comboBox.removeAllItems();
    	for(int i = 0; i < dbList.length; i++) {
    		comboBox.addItem(dbList[i]);
    	}
    }
}
